<?php 
    namespace Mark\Repository;
    
    class MarkRepository extends AbstractRepository {
        protected  $entity="Mark\\Entity\\Mark";
        
        public function listLimit($uid, $number=null){
            $dql = "SELECT c FROM $this->entity c JOIN c.user u WHERE u.uid = ?1 order by c.id desc";
            $query = $this->getEntityManager()->createQuery($dql);
            $query->setParameter(1, $uid);
            if($number!=null && $number>0) $query->setMaxResults($number);
            return $query->getResult();
        }
        
        public function listFromMin($uid, $minId, $maxResult=null, $excludeMin=true){
            $quantifier = $excludeMin?">":">=";
            $dql = "SELECT c FROM $this->entity c JOIN c.user u WHERE u.uid = ?2 and c.id ".$quantifier." ?1 order by c.id asc";
            $query = $this->getEntityManager()->createQuery($dql)
                ->setParameter(1, $minId)
                ->setParameter(2, $uid);
            if($maxResult!=null && $maxResult>0) $query->setMaxResults($maxResult);
            return $query->getResult();
        }
        
        
        public function listFromMax($uid, $maxId, $maxResult=null, $excludeMax=true){
            $quantifier = $excludeMax?"<":"<=";
            $dql = "SELECT c FROM $this->entity c JOIN c.user u WHERE u.uid = ?2 and c.id ".$quantifier." ?1 order by c.id desc";
            $query = $this->getEntityManager()->createQuery($dql)
                ->setParameter(1, $maxId)
                ->setParameter(2, $uid);
            if($maxResult!=null && $maxResult>0) $query->setMaxResults($maxResult);
            return $query->getResult();
        }
        
        
        public function listByUser($idUser, $maxResult=null){
            $dql = "SELECT c FROM $this->entity c JOIN c.user_uid u WHERE u.uid = ?1";
            $query = $this->getEntityManager()->createQuery($dql)
                ->setParameter(1, $idUser);
            if($maxResult!=null && $maxResult>0) $query->setMaxResults($maxResult);
            return $query->getResult();
        }
        
        
    }
